package com.spudtech.collision
{
    
    import flash.geom.*;
    import flash.display.*;
    
    import com.spudtech.*;
    
    
    public class SpudRect extends SpudCollider
    {
        
        public var width : Number;
        public var height : Number;
        
        public function SpudRect(position : Point,
                                 size : Point)
        {
            super(position);
            
            this.width = size.x;
            this.height = size.y;
            
        }
        
        public function get flashRect() : Rectangle
        {
            //return new Rectangle(this.left, this.top, 600, 500);
            return new Rectangle(this.left, this.top, this.width, this.height);
        }
        
        public function get left() : Number
        {
            return this.x;
        }
        public function set left(new_left : Number) : void
        {
            this.x = new_left;
        }
        public function get right() : Number
        {
            return left + width;
        }
        public function set right(new_right : Number) : void
        {
            left = new_right - width;
        }
        public function get centerx() : Number
        {
            return left + (width / 2.0);
        }
        public function set centerx(new_centerx : Number) : void
        {
            left = new_centerx - (width / 2.0);
        }
        
        public function get top() : Number
        {
            return this.y;
        }
        public function set top(new_top : Number) : void
        {
            this.y = new_top;
        }
        public function get bottom() : Number
        {
            return top + height;
        }
        public function set bottom(new_bottom : Number) : void
        {
            top = new_bottom - height;
        }
        public function get centery() : Number
        {
            return top + (height / 2.0);
        }
        public function set centery(new_centery : Number) : void
        {
            top = new_centery - (height / 2.0);
        }
        
        override public function containsPoint(point : Point) : Boolean
        {
            var coll_x : Boolean = true;
            var coll_y : Boolean = true;
            
            coll_x = coll_x && this.left < point.x;
            coll_x = coll_x && this.right > point.x;
            
            coll_y = coll_y && this.top < point.y;
            coll_y = coll_y && this.bottom > point.y;
            
            return coll_x && coll_y;
            
        }
        
        override public function overlaps(other : SpudCollider) : Boolean
        {
            if (other is SpudRect)
            {
                var coll_x : Boolean = true;
                var coll_y : Boolean = true;
                
                var other_rect : SpudRect = SpudRect(other);
                
                coll_x = coll_x && this.left < other_rect.right;
                coll_x = coll_x && other_rect.left < this.right;
                
                coll_y = coll_y && this.top < other_rect.bottom;
                coll_y = coll_y && other_rect.top < this.bottom;
                
                return coll_x && coll_y;
                
            }
            else if (other is SpudGrid)
            {
                // TODO
                return false;
            }
            else
            {
                throw new TypeError("Cannot check for overlap with given collider");
            }
            
            return false;
            
        }
        
        override public function clampIn(boundary : SpudCollider) : void
        {
            if (boundary is SpudRect)
            {
                var boundary_rect : SpudRect = SpudRect(boundary);
                
                if (this.width > boundary_rect.width)
                {
                    this.centerx = boundary_rect.centerx;
                }
                else if (this.left < boundary_rect.left)
                {
                    this.left = boundary_rect.left;
                }
                else if (this.right > boundary_rect.right)
                {
                    this.right = boundary_rect.right;
                }
                
                if (this.height > boundary_rect.height)
                {
                    this.centery = boundary_rect.centery;
                }
                else if (this.top < boundary_rect.top)
                {
                    this.top = boundary_rect.top;
                }
                else if (this.bottom > boundary_rect.bottom)
                {
                    this.bottom = boundary_rect.bottom;
                }
                
            }
            else if (boundary is SpudGrid)
            {
                // TODO
            }
            else
            {
                throw new TypeError("Cannot clamp into given collider");
            }
        }
        
        override public function clampOff(barrier : SpudCollider) : void
        {
            if (barrier is SpudRect)
            {
                
                var delta_x : Number;
                var delta_y : Number;
                
                var other_rect : SpudRect = SpudRect(barrier);
                
                // How far left would we have to move?
                var delta_left : Number = this.left - other_rect.left;
                // How far right would we have to move?
                var delta_right : Number = this.right - other_rect.right;
                
                // How far up would we have to move?
                var delta_up : Number = this.top - other_rect.top;
                // How far down would we have to move?
                var delta_down : Number = this.bottom - other_rect.bottom;
                
                if (Math.abs(delta_left) < Math.abs(delta_right))
                {
                    // Going left would be shorter
                    delta_x = delta_left;
                }
                else
                {
                    // Going right would be shorter
                    delta_x = delta_right;
                }
                
                if (Math.abs(delta_up) < Math.abs(delta_down))
                {
                    // Going up would be shorter
                    delta_x = delta_up;
                }
                else
                {
                    // Going down would be shorter
                    delta_x = delta_down;
                }
                
                
                if (Math.abs(delta_x) < Math.abs(delta_y))
                {
                    // Moving horizontally would be shorter
                    this.x += delta_x;
                }
                else
                {
                    // Moving vertically would be shorter
                    this.y += delta_y;
                }
                
            }
            else if (barrier is SpudGrid)
            {
                // TODO
            }
            else
            {
                throw new TypeError("Cannot clamp off of given collider");
            }
        }
        
    }
    
}
